import pytest

from appium.webdriver.common.appiumby import AppiumBy
import time

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import subprocess
import os
from selenium.common.exceptions import TimeoutException

#切换webview_context
def test_switch_to_webview_context(driver):
    contexts = driver.contexts
    webview_contexts = [ctx for ctx in contexts if 'WEBVIEW' in ctx]
    return webview_contexts[0] if webview_contexts else False
    try:
        wait = WebDriverWait(driver, 10)
        webview_context = wait.until(webview_context_available)
        driver.switch_to.context(webview_context)
    except TimeoutException:
        print("Timeout waiting for WebView context to become available.")

def test_login(driver):
    try:
        wait = WebDriverWait(driver, 10)  # 等待最多 10 秒
        elements = wait.until(EC.presence_of_element_located((AppiumBy.ID, 'uni.UNI31459F4:id/moreBrandId')))
        # for item in items:
        elements[0].click()

        # 定位一个有特定data-v类的元素
        # element = driver.find_element_by_xpath('//android.webkit.WebView/android.view.View[@content-desc="data-v-unique-id"]')

        # 使用XPath定位一个有特定文本的元素
        # element = driver.find_element_by_xpath('//android.webkit.WebView/android.view.View[@content-desc="Login"]')


      # 使用ID定位元素
        # element = wait.until(EC.presence_of_element_located((By.ID, 'com.example.app:id/button_login')))
        # assert element.is_displayed(), "Element not found"

        # 获取按钮的文本
        # button_text = element.tag_name
        # 断言按钮文本
        # self.assertEqual(button_text, "哈哈哈哈", "------按钮显示内容不正确-------------")
        # 输出按钮文本
        # print(f"=================点击按钮的名称：===================Button text: {button_text}")
        
        
    except Exception as e:
        print(f"Test failed: {e}")
        # 如果发生异常，则跳过后续的测试用例
        # raise unittest.SkipTest("Failed to find element: " + str(e))
        raise

# def test_switch_to_webview_context(driver):
#     contexts = driver.contexts
#     # 获取所有可用的上下文， 切换到 WebView 上下文
#     webview_context = [context for context in contexts if 'WEBVIEW' in context][0]
#     driver.switch_to.context(webview_context)

#     try:
#         wait = WebDriverWait(driver, 10)
#         webview_context = wait.until(EC.presence_of_element_located(lambda d: [ctx for ctx in d.contexts if 'WEBVIEW' in ctx]))
#         driver.switch_to.context(webview_context[0])
#     except TimeoutException:
#         print("Timeout waiting for WebView context to become available.")

    # 切换回Native上下文
    # native_context = [context for context in contexts if 'NATIVE_APP' in context][0]
    # driver.switch_to.context(native_context)


# def test_click_button_and_verify_content(driver):
#     # 等待应用启动
#     time.sleep(5)
#     print(f"==================应用成功启动================")
#     # 查找按钮元素
#     wait = WebDriverWait(driver, 20)  # 等待最多 10 秒
#     element = wait.until(EC.presence_of_element_located((AppiumBy.ID, "navigation_dashboard")))
#     element.click()
    
 
#     # 获取按钮的文本
#     button_text = element.tag_name
#     # 输出按钮文本
#     print(f"=================点击按钮的名称：===================Button text: {button_text}")
#     # 断言按钮文本
#     assert "哈哈哈哈" in button_text,  "------按钮显示内容不正确-------------"

# 在模块末尾手动调用 cleanup_resources 函数
# if __name__ == '__main__':
    # pytest.main(['-s', 'd:/project/vscode/BestTest/tests/mobile_tests/android_tests/e2e_tests/test_home_to_login_pytest.py'])


    